﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meraz.Core.Model;
using Meraz.Core.Repositories;
using Ninject;
using Meraz.Core.Data;
namespace Meraz.Core.Commands
{
    public interface ICreateTopicsCommand { 
        Topic Execute(string topicName); 
        IDictionary<string, Topic> Execute(IEnumerable<string> topicNames); 
    }
    public class CreateTopicsCommand : BaseCommand, ICreateTopicsCommand
    {
        [Inject]
        public ITopicRepository TopicRepository { get; set; }

        public Topic Execute(string topicName)
        {
            IDictionary<string, Topic> dict = Execute(new string[] { topicName });
            return dict[topicName];
        }
        public IDictionary<string, Topic> Execute(IEnumerable<string> topicNames)
        {
            Dictionary<string, Topic> dict = new Dictionary<string, Topic>();
            for (int topicNameIndex = 0; topicNameIndex < topicNames.Count(); topicNameIndex++)
            {
                string topicName = topicNames.ElementAt(topicNameIndex);
                Topic entity = TopicRepository.Get(topicName);
                if (entity == null)
                {
                    entity = new Topic();
                    TopicRepository.Add(entity);
                    entity.Name = topicName;
                }
                dict[topicName] = entity;
            }
            Commit();
            return dict;
        }
    }
}
